package com.sapient;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class MappingServlet
 */
@WebServlet("/MappingServlet")
public class MappingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MappingServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String[] mapType=request.getParameterValues("type");
		HttpSession session= request.getSession(true);
		session.setAttribute("Type", mapType[0]);
		
		//		PrintWriter pw= response.getWriter();

		//		pw.write(mapType[0]);
		//		HttpSession session=request.getSession();
		//		session.setAttribute("Type", mapType[0]);
		request.setAttribute("Type", mapType[0]);
		RequestDispatcher r = getServletContext().getRequestDispatcher("/MappingSearch.jsp");
		r.forward(request, response);


	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String url = "jdbc:oracle:thin:@10.207.60.85:1521/XE";
		String username = "surv";
		String password = "surv";

//		String df = "MM/dd/yyyy";
//		Date date =new Date();
//		SimpleDateFormat sf = new SimpleDateFormat(df);



		HttpSession session= request.getSession();
		String mapType=(String)session.getAttribute("Type");

		String mapEntity=request.getParameter("Entity");
		String mapString=request.getParameter("String");
		String mapDescription=request.getParameter("Description");
		String mapStartDate=request.getParameter("StartDate");
		String mapEndDate=request.getParameter("EndDate");
		String mapSource=request.getParameter("Source");

		PrintWriter pw=response.getWriter();
		
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Connection conn = DriverManager.getConnection(url, username,
					password);
			Statement st = conn.createStatement();

			//				String SQL = "INSERT INTO person VALUES (5,'abcdefg', 300)";
			//				st.executeUpdate(SQL);

			//System.out.println(mapString +   mapEntity  + sf.format(date)+ mapSource + mapType + mapDescription);
			
			//System.out.println("INSERT INTO EntityMapping (MapId, MappingString, MappingEntity, StartDate, SourceSystem, IsEnabled,entitytype,lastupdateduser,mappingdescription) VALUES (mappingseq.nextval,'"+ mapString + "','" + mapEntity + "','" + sf.format(date)+ "','" + mapSource + "','" + "'Y'"+ "','" + mapType + "','" + "User" + "','"+ mapDescription+ "')");
			//to_date(mapStartDate,'MM/DD/YYYY')	
			
	//		st.executeUpdate("INSERT INTO EntityMapping (MapId, MappingString, MappingEntity, StartDate, SourceSystem, IsEnabled,entitytype,lastupdateduser,mappingdescription) VALUES (mappingseq.nextval,'"+ mapString + "','" + mapEntity + "'," + "CURRENT_DATE" + ",'" + mapSource + "','" + "Y"+ "','" + mapType + "','" + "user" + "','"+ mapDescription+ "')");
			
			String SQL1="select mappingentity, mappingstring from entitymapping where mappingentity='" + mapEntity+"' and mappingstring = '"+mapString+"'";
			Statement st1=conn.createStatement();
			ResultSet rs1=st1.executeQuery(SQL1);
			System.out.println(SQL1);
	//		System.out.println(rs1.next());
			if(rs1.next()==false){
			st.executeUpdate("INSERT INTO EntityMapping (MapId, MappingString, MappingEntity, StartDate, SourceSystem,entitytype,lastupdateduser,mappingdescription,enddate,isenabled,status) VALUES (mappingseq.nextval,'"+ mapString + "','" + mapEntity + "'," + "to_date('" + mapStartDate + "','RRRR-MM-DD')" + ",'" + mapSource + "','" + mapType + "','" + "user" + "','"+ mapDescription+"',"+ "to_date('" + mapEndDate + "','RRRR-MM-DD')" +  ",'Y','P')");
			RequestDispatcher r = request.getRequestDispatcher("/MappingAlert.jsp");
			r.forward(request, response);
			
			}
			
			else
			{
	//			response.setContentType("text/html");
	//			pw.write("Mapping entity already exists");
				RequestDispatcher r = request.getRequestDispatcher("/MappingDuplication.jsp");
				r.forward(request, response);
				
			}
			
			//			"to_date('" + mapStartDate + "','MM/DD/YYYY')" Varun's Solution
																																				
			/*ResultSet rs = st.executeQuery("SELECT * FROM entitymapping");
			while (rs.next()) {
				int ID = rs.getInt("mapid");
				String mstring = rs.getString("mappingstring");
				System.out.println(ID + "\t" + mstring );
			}*/

//			response.setContentType("text/html");
//			pw.write("<h1>Mapping Updated Successfully</h1>");

			
			conn.close();

		} catch (Exception e) {
			System.err.println("Got an exception! ");
			System.err.println(e.getMessage());
			e.printStackTrace();
		}

	}

}
